/*
* $Id: ServiceDeleteOperation.cpp 336 2011-09-28 06:06:36Z volok-aleksej@yandex.ru $
*
* Copyright 2009-2011 by Volokitin Aleksey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ServiceDeleteOperation.h"
#include "Database/TransactManager.h"
#include "Database/Tables/JournalTable.h"
#include "Database/Tables/FamiliesTable.h"
#include "Database/Operation/DeleteOperation.h"

CServiceDeleteOperation::CServiceDeleteOperation(CTransactManager* pManager, CDeleteOperation::content::type type)
: m_pManager(pManager), m_type(type){}
CServiceDeleteOperation::~CServiceDeleteOperation(){}

std::tstring CServiceDeleteOperation::ExecuteOperation(IDatabase* pDatabase, IReader* pReader){
	int size;
	pReader->ReadInt(size);
	int id;
	pReader->ReadInt(id);
	std::vector<int> ids;
	std::tstring strLog;
	switch (m_type)
	{
	case CDeleteOperation::content::POINT:
		id = m_pManager->GetRealId(CTransactManager::ID(CTransactManager::POINT, id));
		DeletePoint(pDatabase, id);
		strLog = ILanguageManager::GetLanguageManager()->GetString(DELETE_POINT_LOG_ID);
		break;
	case CDeleteOperation::content::FAMILY:
		id = m_pManager->GetRealId(CTransactManager::ID(CTransactManager::FAMILY, id));
		DeleteFamily(pDatabase, id);
		strLog = ILanguageManager::GetLanguageManager()->GetString(DELETE_FAMILY_LOG_ID);
		break;
	case CDeleteOperation::content::JOURNAL:
		id = m_pManager->GetRealId(CTransactManager::ID(CTransactManager::JOURNAL, id));
		DeleteJournal(pDatabase, id);
		strLog = ILanguageManager::GetLanguageManager()->GetString(DELETE_INSPECT_LOG_ID);
		break;
	case CDeleteOperation::content::QUEEN_BEE:
		id = m_pManager->GetRealId(CTransactManager::ID(CTransactManager::QUEEN_BEE, id));
		DeleteQueenBee(pDatabase, id);
		strLog = ILanguageManager::GetLanguageManager()->GetString(DELETE_QUEEN_BEE_LOG_ID);
		break;
	case CDeleteOperation::content::HOUSING:
		id = m_pManager->GetRealId(CTransactManager::ID(CTransactManager::HOUSING, id));
		DeleteHousing(pDatabase, id);
		strLog = ILanguageManager::GetLanguageManager()->GetString(DELETE_HOUSING_LOG_ID);
		break;
	}
	return strLog;
}

void CServiceDeleteOperation::Free(){
	delete this;
}



void CServiceDeleteOperation::DeletePoint(IDatabase* pDatabase, int id){
	CFamiliesTable table;
	table.m_content.s_nIdPoint = id;
	IResult* pResult = table.GetValue(pDatabase);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CTable::content* pContent = (CTable::content*)(*pResult)[i];
		DeleteFamily(pDatabase, pContent->s_nId);
	}
	pResult->Free();
	CDeleteOperation operation;
	operation.m_content.s_nId = id;
	operation.m_content.s_type = CDeleteOperation::content::POINT;
	operation.DeleteValue(pDatabase);
}
void CServiceDeleteOperation::DeleteFamily(IDatabase* pDatabase, int id){
	CJournalTable table;
	table.m_content.s_nIdFamily = id;
	IResult* pResult = table.GetValue(pDatabase);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CTable::content* pContent = (CTable::content*)(*pResult)[i];
		DeleteJournal(pDatabase, pContent->s_nId);
	}
	pResult->Free();
	CDeleteOperation operation;
	operation.m_content.s_nId = id;
	operation.m_content.s_type = CDeleteOperation::content::FAMILY;
	operation.DeleteValue(pDatabase);
}
void CServiceDeleteOperation::DeleteJournal(IDatabase* pDatabase, int id){
	CDeleteOperation operation;
	operation.m_content.s_nId = id;
	operation.m_content.s_type = CDeleteOperation::content::JOURNAL;
	operation.DeleteValue(pDatabase);
}

void CServiceDeleteOperation::DeleteHousing(IDatabase* pDatabase, int id){
	CDeleteOperation operation;
	operation.m_content.s_nId = id;
	operation.m_content.s_type = CDeleteOperation::content::HOUSING;
	operation.DeleteValue(pDatabase);
}
void CServiceDeleteOperation::DeleteQueenBee(IDatabase* pDatabase, int id){
	CDeleteOperation operation;
	operation.m_content.s_nId = id;
	operation.m_content.s_type = CDeleteOperation::content::QUEEN_BEE;
	operation.DeleteValue(pDatabase);
}
